#!/usr/bin/python3
# -*- coding utf-8 -*-
# @Description: 爬虫主函数
# @Author: diyhome
# @Date: 2021/1/22 9:05
import json
import requests
import csv
import time
import random

target_url = 'http://data.eastmoney.com/dataapi/stockdata/f10api'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
}
params_ = {
    'type': 'PCF10/RptCompanyProfile',
    'postData': '{"SecurityCode": "600005.SZ"}',
    'fields': 'CorpName,Representative,RegistryAddress,RegistryCapital,BusinessRange,CorpSummary'
}


def get_raw_json(index, exchange_name='SZ'):
    params_['postData'] = '{"SecurityCode": "%s.%s"}' % (str(index).zfill(6), exchange_name)
    raw_json = requests.get(target_url, headers=headers, params=params_).text
    list_json = json.loads(raw_json)
    RegistryAddress = list_json['data'][0]['RegistryAddress']
    CorpName = list_json['data'][0]['CorpName']
    return [CorpName, RegistryAddress]


def allin_get_registry(index):
    get_data = []
    get_data.append(str(index).zfill(6))
    ans = get_raw_json(index, 'SZ')
    if ans[0] == "":
        ans = get_raw_json(index, 'SH')

    if ans[0] == "":
        hk_url = 'http://emweb.securities.eastmoney.com/PC_HKF10/CompanyProfile/PageAjax'
        hk_params = {'code': str(index).zfill(5)}
        raw_json = requests.get(hk_url, headers=headers, params=hk_params).text
        list_json = json.loads(raw_json)
        if 'msg' in list_json:
            return [index, '', '']
        RegistryAddress = list_json['gszl']['zcdz']
        CorpName = list_json['gszl']['gsmc']
        ans = [CorpName, RegistryAddress]
        get_data[0] = str(index).zfill(5)
    get_data.extend(ans)
    return get_data


if __name__ == '__main__':
    f = open('抓取到的结果.csv', 'a', encoding='utf-8', newline='')
    csv_writer = csv.writer(f)

    i = 1
    while i < 688981:
        print("downloading code is: " + str(i))
        ans = allin_get_registry(i)
        i += 1
        if ans[1] == "":
            continue
        csv_writer.writerow(ans)
        # time.sleep(random.randint(0, 2))
    f.close()
